#include <stdio.h>
#include <malloc.h>

// Func to appends a value in list
void append(int *arr,int n)
{ int x;
  printf("enter the element you want to append:\n");
  scanf("%d",&x);
  *(arr+n)=x;
}

// func to reverse a given array
void reverse(int *arr, int n)
{
int i,j,k;
for (i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
{
k=arr[i];
arr[i]=arr[j];
arr[j]=k;
}

}

// func to sort an array
void sort(int *arr, int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]= arr[j];
arr[j]=temp;
}
}
}

}


// Func to insert a value in the list
void insert(int *arr,int n,int x, int in)
{
int i;
if(in>n|in==0)
printf("index out of range\n");
else
{
for(i=n;i>in;i--)
*(arr+i)=*(arr+i-1);
*(arr+in)=x;
}
}

// func to delete a value at a given index
int delete (int *arr, int n,int in)
{
int i,j,k;
if(in>n|in==0)
{
printf("index out of range\n");
k=0;
}
else
{
for(i=0;i<n;i++)
{
if(i == in-1)
	{
	k=arr[i];
	for(j=i;j<n-1;j++)
	arr[j]=arr[j+1];
	break;
}
}
}
return k;
}

// func to remove a given value
int remove1 (int *arr, int n,int x)
{
int i,j,counter=0;
for(i=0;i<n;i++)
{
	if (i==n-1 && arr[n-1]!=x)
	{
	counter=1;
	printf("element is not in the list \n");
	break;
	}
	else
	{
	if(arr[i]==x)
	{
	for(j=i;j<n-1;j++)
	arr[j]=arr[j+1];
	break;
	}
	}
}
return counter;

}

// func to extend a list by appending values from another list
void extend(int *arr1,int n1,int n2)
{
int b[n2],i,k,j;
printf("enter elements of other array:\n");
  for(i=0;i<n2;i++)
  {
   scanf("%d",&b[i]); 
  }
  for(k=n1,j=0;k<(n1+n2),j<n2;k++,j++)
  *(arr1+k)=b[j];
}

// func to display elements of array
void display(int *arr, int n)
{
int i;
printf("the list is as folows:\n");
for(i=0;i<n;i++)
printf("%d \n",arr[i]);
}

// function to find maximum and minimum element
void maxmin(int *arr,int n)
{
int max,min,i;
max=arr[0];
min=arr[0];
for(i=0;i<n;i++)
{
if (max<arr[i])
	max=arr[i];
if (min>arr[i])
	min=arr[i];
}
printf("the maximum no is: %d \n",max);
printf("the minimum no is: %d \n",min);
}

// func to find count of a value
int count(int *arr,int n)
{
int x,i,counter=0;
printf("enter the element of which u want the count \n");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if (x==arr[i])
{	
counter++;
}
}
if(i==n)
{
printf("element is not in the list \n");
return 0;
}
else
return counter;
}

// func to find index of a given value
int index(int *arr,int n)
{
int x,i,counter=0;
printf("enter the element of which u want the index \n");
scanf("%d",&x);
for (i=0;i<n;i++)
{
if(x==arr[i])
{
	counter=i;
	break;
}
}
if(i==n )
{
printf("element is not in the list \n");
return 0;
}
else
return (counter+1);
}


//main
void  main()
{
int arr[50],n,i,choice,x,in,counter,n1,arr2[50];
printf("enter the length of array \n");
scanf("%d",&n);
printf("enter the elements of array(not more than 50) \n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("elements of array are as follows:\n");
display(arr,n);
printf("enter 0 to append,1 to insert,2 to pop,3 to remove the element,4 to extend a list,5 to find max and min value, 6 to find count of given value,7 to find index of given value,8 to reverse an array, 9 to sort an array  \n");
scanf("%d",&choice);
switch(choice)
{
case 0:
append(arr,n);
n=n+1;
display( arr,n);
break;

case 1:
printf("enter the value u want to insert:\n");
scanf("%d",&x);
printf("enter the index at which u want to insert:\n");
scanf( "%d",&in);
insert(arr,n,x,in);
n=n+1;
display(arr,n);
break;

case 2:
printf("enter the index of value u want to delete: \n");
scanf("%d",&in);
x=delete(arr,n,in);
if(x!=0)
{
n=n-1;
printf("element u poped is: %d\n",x);
}
display(arr,n);
break;

case 3:
printf("enter the value u want to delete :\n");
scanf("%d",&x);
counter = remove1(arr,n,x);
if (counter!=0)
{
n=n-1;
printf("element u poped is: %d\n",counter);
}
display(arr,n);
break;

case 4:
printf("enter the length of other array:\n");
scanf("%d",&n1);

extend(arr,n,n1);
n=n+n1;
display(arr,n);
break;

case 5:
maxmin(arr,n);
break;

case 6:
counter=count(arr,n);
printf("the count of element is %d\n",counter);
break;

case 7:
counter=index(arr,n);
printf("index of the element is %d\n",counter);
break;

case 8:
reverse(arr,n);
display(arr,n);
break;

case 9:
sort(arr,n);
display(arr,n);
break;

}
}


